package com.apkicon;

import com.apkicon.core.ApkIconExtractor;
import com.apkicon.model.IconInfo;
import com.apkicon.quality.IconQualityAnalyzer;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.File;
import java.util.concurrent.Callable;

@Command(name = "apk-icon-extractor", 
         mixinStandardHelpOptions = true, 
         version = "1.0.0",
         description = "Extract and convert APK icons to PNG format")
public class Main implements Callable<Integer> {

    @Parameters(index = "0", description = "APK file path")
    private File apkFile;

    @Option(names = {"-o", "--output"}, description = "Output directory (default: current directory)")
    private File outputDir = new File(".");

    @Option(names = {"-s", "--size"}, description = "Output PNG size (default: 512)")
    private int size = 512;

    @Option(names = {"-v", "--verbose"}, description = "Verbose output")
    private boolean verbose = false;
    
    @Option(names = {"-q", "--quality"}, description = "Show quality analysis")
    private boolean showQuality = false;

    public static void main(String[] args) {
        int exitCode = new CommandLine(new Main()).execute(args);
        System.exit(exitCode);
    }

    @Override
    public Integer call() throws Exception {
        if (!apkFile.exists()) {
            System.err.println("APK file not found: " + apkFile.getAbsolutePath());
            return 1;
        }

        if (!outputDir.exists()) {
            outputDir.mkdirs();
        }

        System.out.println("Extracting icon from: " + apkFile.getName());
        System.out.println("Output directory: " + outputDir.getAbsolutePath());
        System.out.println("Output size: " + size + "x" + size);

        try {
            ApkIconExtractor extractor = new ApkIconExtractor();
            
            if (verbose) {
                System.out.println("Bundletool available: " + extractor.isBundletoolAvailable());
            }
            
            IconInfo iconInfo = extractor.extractIcon(apkFile, size);
            
            if (iconInfo != null) {
                File outputFile = new File(outputDir, apkFile.getName().replace(".apk", "_icon.png"));
                iconInfo.saveAsPng(outputFile);
                System.out.println("Icon extracted successfully: " + outputFile.getName());
                
                if (verbose) {
                    System.out.println("Icon type: " + iconInfo.getIconType());
                    System.out.println("Original size: " + iconInfo.getOriginalWidth() + "x" + iconInfo.getOriginalHeight());
                    System.out.println("Output size: " + iconInfo.getImage().getWidth() + "x" + iconInfo.getImage().getHeight());
                }
                
                if (showQuality) {
                    System.out.println("\n=== 图标质量分析 ===");
                    IconQualityAnalyzer.QualityReport qualityReport = IconQualityAnalyzer.analyzeIcon(iconInfo.getImage());
                    System.out.println(qualityReport.toString());
                }
            } else {
                System.err.println("No icon found in APK");
                return 1;
            }
        } catch (Exception e) {
            System.err.println("Error extracting icon: " + e.getMessage());
            if (verbose) {
                e.printStackTrace();
            }
            return 1;
        }

        return 0;
    }
}
